//
//  ZQstatusTableViewCell.swift
//  ZQWeibo
//
//  Created by summer的Dad on 16/11/17.
//  Copyright © 2016年 summer的Dad. All rights reserved.
////屏幕的尺寸信息
//let ScreenWidth = UIScreen.main.bounds.width
//
//let ScreenHeight = UIScreen.main.bounds.height


import UIKit
import SDWebImage
import YYText


  private let margin : CGFloat = 8

  private let maxWith : CGFloat = ScreenWidth - 2 * margin

  private let imgMargin : CGFloat = 2

  private let itmeWight : CGFloat = ( maxWith - 2 * imgMargin) / 3


class ZQstatusTableViewCell: UITableViewCell {

    //头像
    @IBOutlet weak var iconImage: UIImageView!
    //名字
    @IBOutlet weak var nameLable: UILabel!
    //时间
    @IBOutlet weak var timeLable: UILabel!
    //来源
    @IBOutlet weak var source: UILabel!
    //内容
    @IBOutlet weak var contentLable: YYLabel!
    //用户等级
    @IBOutlet weak var mbRankView: UIImageView!

    //用户类型
    @IBOutlet weak var avatarView: UIImageView!
    //collection
    @IBOutlet weak var pictureView: PictureView!
    //collectionHeight
    @IBOutlet weak var pictureHeigth: NSLayoutConstraint!
    //collectionWigth
    @IBOutlet weak var pictureWigth: NSLayoutConstraint!
    //flowLayout
    @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout!
    /////////--------转发内容--------------------------------
    //顶部配图
    @IBOutlet weak var pictureViewtopCnst: NSLayoutConstraint!
    //转发内容
    @IBOutlet weak var retweetedLable: YYLabel!
    //底部工具栏
    @IBOutlet weak var toolBar: UIView!

    
 //绑定属性
    var viewmodel : ZQstatusViewModel? {
        didSet{
            
            iconImage.sd_setImage(with: viewmodel?.iconURL)
            nameLable.text = viewmodel?.status?.user?.name
            
            timeLable.text = viewmodel?.timeText
            source.text = viewmodel?.sourceText
            contentLable.attributedText = viewmodel?.originateString
            //用户等级
            mbRankView.image = viewmodel?.verifitedImage
            avatarView.image = viewmodel?.mbrankImage
            //拿到图片的张数
            let count = viewmodel?.imageUrls?.count ?? 0
            
            //  let count = viewmodel?.status?.pic_urls?.count ?? 0
            //修改顶部视图的约束
            //pictureViewtopCnst.constant = (count == 0 ? 0 : margin)
            
            let pSize = calculatePictureViewSize(count: count)
            //设置约束
            pictureWigth.constant = pSize.pSize.width
            pictureHeigth.constant = pSize.pSize.height
            //还需要更新一下约束
            collectionViewFlowLayout.itemSize = pSize.itmeSize
  
            //配图视图转发还是原创
            pictureView.pictureInfo = viewmodel?.imageUrls
     
            //配图文本
            retweetedLable?.attributedText = viewmodel?.relayString
            
           pictureView.layoutIfNeeded()
           contentLable.highlightTapAction = { (containerView,text,range,rect) -> () in
                //containerView,就是被点击的视图
                //text: 被点击的属性文本
                //range: 点击的特殊文本 范围
                //rect: 点击的特殊的文本的坐标
                let sub = (text.string as NSString).substring(with: range)
            
                //跳转页面   响应者链条来实现页面跳转
            if sub.hasPrefix("http") {
                
                let webView = ZQWebViewController()
                
                webView.urlString = sub
                
                self.findNavController()?.pushViewController(webView, animated: true)
                
            }
            
            
            
            }
      
        }
    
    }
    
    
    
    
    
    //两个参数的叫元祖类型
    //设置图片大小
    func calculatePictureViewSize(count : Int) -> (pSize : CGSize , itmeSize : CGSize) {
        
        if count == 0 {
            return (CGSize(width: 1, height: 1),CGSize(width: 1, height: 1))
        }
        if count == 1 {
           
            if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewmodel?.imageUrls?.last?.wep360 ?? ""){
                
                let imageSize = image.size
                
               return (imageSize,imageSize)
            }
            return (CGSize(width: itmeWight, height: itmeWight),CGSize(width: itmeWight, height: itmeWight))
        
        }
      if count == 4 {
            
            let with = itmeWight * 2 + margin
            return (CGSize(width: with, height: with),CGSize(width: with, height: with))
        }
     
        let row = CGFloat((count - 1) / 3) + 1
        
        let h = (row - 1) * imgMargin + row * itmeWight
        
        return (CGSize(width: maxWith, height: h),CGSize(width: itmeWight , height: itmeWight))
        
    }
    //设置Item大小
    override func awakeFromNib() {
        super.awakeFromNib()
     
//        self.contentView.backgroundColor = randomColor()
        self.contentLable.numberOfLines = 0
        self.contentLable.preferredMaxLayoutWidth = ScreenWidth - 2 * margin
        
        collectionViewFlowLayout.itemSize = CGSize(width: itmeWight, height: itmeWight)
        
        collectionViewFlowLayout.minimumInteritemSpacing = imgMargin
        collectionViewFlowLayout.minimumLineSpacing = imgMargin
        //转发设置
         self.retweetedLable?.numberOfLines = 0
        self.retweetedLable?.preferredMaxLayoutWidth = ScreenWidth - 2 * margin
     
        //选中取消
            selectionStyle = .none
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
